$(function() {
	// 显示文章列表信息
	showAticleInfoList();
	function showAticleInfoList() {
		var params = new Object();
		params.index = $("#aticleInfoListTable").attr("data-index");
		initAticlePage(params);
	}
	function initAticlePage(params){
		$.post("listAticleInfoByPage.action",
				params,
				function(data) {
					var aticleInfoList = data.content;
					// 分页文章
					if (aticleInfoList.length != 0) {
						aticleListPage(aticleInfoList);
					}
					// 分页模块
					var aticlePageObj = $("#aticleInfoListPage");
					var aticlePageTemplate = "<ul class=\"pagination\">";
					if (data.first) {
						aticlePageTemplate += "<li class=\"disabled\"><a href=\"javascript:;\" aria-label=\"Previous\"> <span aria-hidden=\"true\">上一页</span></a></li>"
					} else {
						aticlePageTemplate += "<li><a id=\"previousAticleList\" href=\"javascript:;\" aria-label=\"Previous\"> <span aria-hidden=\"true\">上一页</span></a></li>"
					}
					for (var i = 0; i < data.totalPages; i++) {
						if (i == params.index) {
							aticlePageTemplate += "<li class=\"commonpage active\"><a href=\"javascript:;\" onclick=\"showCurrentPageList('"
									+ i
									+ "')\">"
									+ (i + 1)
									+ "</a></li>";
						} else {
							aticlePageTemplate += "<li class=\"commonpage\"><a  id=\"previousAticleList\" href=\"javascript:;\" onclick=\"showCurrentPageList('"
									+ i
									+ "')\">"
									+ (i + 1)
									+ "</a></li>";
						}

					}
					if (data.last) {
						aticlePageTemplate += "<li class=\"disabled\"><a href=\"javascript:;\" aria-label=\"Next\"> <span aria-hidden=\"true\">下一页</span></a></li>"
					} else {
						aticlePageTemplate += "<li><a id=\"nextAticleList\" href=\"javascript:;\" aria-label=\"Previous\" onclick=\"showNextList()\"> <span aria-hidden=\"true\">下一页</span></a></li>"
					}
					aticlePageTemplate += "</ul>";
					aticlePageObj.html(aticlePageTemplate);
					dynamicAticlePage();
				});
	}
	function dynamicAticlePage(){
		var currentPage=$("#aticleInfoListTable").attr("data-index");
		var params = new Object();
		//上一页
		$("#previousAticleList").bind("click",function(e){
			params.index =currentPage -1;
			initAticlePage(params);
			$("#aticleInfoListTable").attr("data-index",params.index);
			e.preventDefault();
		});
		//下一页
		$("#nextAticleList").bind("click",function(e){
			params.index=currentPage+1;
			initAticlePage(params);
			$("#aticleInfoListTable").attr("data-index",params.index);
			e.preventDefault();
		});
		//点击页数
		var aticlePageObj = $("#aticleInfoListPage");
		aticlePageObj.find("li[class*='commonpage']").bind("click",function(e){
			params.index=$(this).find("a").text()-1;
			initAticlePage(params);
			$("#aticleInfoListTable").attr("data-index",params.index);
			e.preventDefault();
		});
	}
	function aticleListPage(aticleInfoList) {
		var aticleTableObj = $("#aticleInfoListTable").children("tbody");
		var aticleTemplate = "";
		$.each(aticleInfoList, function(i, aticle) {
			aticleTemplate += "<tr><td>" + aticle.aticlename + "</td><td>"
					+ aticle.aticlecategory + "</td><td>" + aticle.aticledigest
					+ "</td><td>" + aticle.aticlereadnum + "</td><td>"
					+ aticle.keywords + "</td><td>" + aticle.aticlecruser
					+ "</td><td>" + aticle.aticlecrtime + "</td></tr>"
		});
		aticleTableObj.html("").html(aticleTemplate);
	}
});
